package com.wjk.easy.adapter;


import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.wjk.easy.R;
import com.wjk.easy.db.Exam;

import org.litepal.LitePal;
import org.litepal.crud.async.UpdateOrDeleteExecutor;
import org.litepal.crud.callback.UpdateOrDeleteCallback;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

public class DataManageListAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Exam> list;
    private int[] countArr;

    public DataManageListAdapter(Context context, List<Exam> list, int[] countArr) {
        this.context = context;
        this.list = list;
        this.countArr=countArr;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_data_manage, parent,
                false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        //TextView tv_exam_name, tv_group_count,tv_create_time,tv_delete_data,tv_remark;
        final Exam exam = list.get(position);
        MyViewHolder h = (MyViewHolder) holder;
        h.tv_exam_name.setText(exam.getName());
        h.tv_group_count.setText(exam.getGroups()+"");
        DateFormat dateTimeInstance = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String formatTime = dateTimeInstance.format(exam.getCreateTime());
        h.tv_create_time.setText(formatTime);
        h.tv_remark.setText(exam.getRemark());

        if(this.countArr!=null&&this.countArr.length>position){
            h.tv_data_count.setText(this.countArr[position]+"");
        }

        if(position%2==0){
            h.ll_exam_container.setBackgroundColor(context.getResources().getColor(R.color.trans));
        }else{
            h.ll_exam_container.setBackgroundColor(context.getResources().getColor(R.color.colorSelectDarkTrans));
        }

        h.tv_export_data.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(emptyListener!=null){
                    emptyListener.onExportClick(exam);
                }
            }
        });
    }

    public void setEmptyListener(EmptyListener emptyListener){
        this.emptyListener = emptyListener;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    public void setDataList(List<Exam> examList) {
        list.clear();
        list.addAll(examList);
        notifyDataSetChanged();
    }

    /**
     * ViewHolder的类，用于缓存控件
     */
    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_exam_name, tv_group_count, tv_create_time, tv_export_data, tv_remark,tv_data_count;
        LinearLayout ll_exam_container;

        MyViewHolder(View view) {
            super(view);
            tv_exam_name = view.findViewById(R.id.tv_exam_name);
            tv_group_count = view.findViewById(R.id.tv_group_count);
            tv_create_time = view.findViewById(R.id.tv_create_time);
            tv_export_data = view.findViewById(R.id.tv_export_data);
            tv_remark = view.findViewById(R.id.tv_remark);
            ll_exam_container = view.findViewById(R.id.ll_exam_container);
            tv_data_count = view.findViewById(R.id.tv_data_count);
        }
    }
    EmptyListener emptyListener;
    public interface EmptyListener{
        void onEmpty();
        void onExportClick(Exam exam);
    }
}
